package com.yc.reference;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

/**
 * Title:<br>
 * Description: <br>
 * Author:陈圆(chenyuan@bosssoft.com.cn) <br>
 * Date:2020/9/20
 */
public class FourReferenceDemo {

    public static void main(String[] args) {

        // 软引用
        SoftReference softReference = new SoftReference<String>(new String ("soft hello world"));
        System.out.println(softReference.get());
        System.gc();
        System.out.println(softReference.get());

        // 弱引用
        WeakReference weakReference = new WeakReference(new String("weak hello world"));
        System.out.println(weakReference.get());
        System.gc(); // 进行gc回收
        System.out.println(weakReference.get()); // 输出 null

        // 虚引用
        ReferenceQueue referenceQueue = new ReferenceQueue<String>();
        PhantomReference phantomReference = new PhantomReference(new String("phantom hello world"), referenceQueue);
        System.out.println(phantomReference.get());
//        System.gc();
//        System.out.println(phantomReference.get());
    }
}
